package interview;

import java.util.Arrays;

public class interview_16_13 {
    public double[] cutSquares(int[] square1, int[] square2) {
        double[] point1=new double[]{square1[0]+square1[2]/2.0,square1[1]+square1[2]/2.0};
        double[] point2=new double[]{square2[0]+square2[2]/2.0,square2[1]+square2[2]/2.0};

        if(point1[0]==point2[0]){
            return new double[]{point1[0],Math.min(square1[1],square2[1]),point1[0],Math.max(square1[1]+square1[2],square2[1]+square2[2])};

        }
        if(point1[1]==point2[1]){
            return new double[]{Math.min(square1[0],square2[0]),point1[1],Math.max(square1[0]+square1[2],square2[0]+square2[2]),point1[1]};
        }
        double k=(point2[1]-point1[1])/(point2[0]-point1[0]);
        double b=point2[1]-k*point2[0];
        double[] result=new double[4];
        if(k<=1&&k>0){
        double[] result1=new double[]{square1[0],k*square1[0]+b};
        double[] result2=new double[]{square1[0]+square1[2],b+k*(square1[0]+square1[2])};
        double[] result3=new double[]{square2[0],k*square2[0]+b};
        double[] result4=new double[]{square2[0]+square2[2],b+k*(square2[0]+square2[2])};

        if(result1[0]<result3[0]){
            result[0]=result1[0];
            result[1]=result1[1];
        }else{
            result[0]=result3[0];
            result[1]=result3[1];
        }
        if(result2[0]<result4[0]){
            result[2]=result4[0];
            result[3]=result4[1];
        }else{
            result[2]=result2[0];
            result[3]=result2[1];
        }
        }else if(k>=-1&&k<0){
            double[] result1=new double[]{square1[0],k*square1[0]+b};
            double[] result2=new double[]{square1[0]+square1[2],b+k*(square1[0]+square1[2])};
            double[] result3=new double[]{square2[0],k*square2[0]+b};
            double[] result4=new double[]{square2[0]+square2[2],b+k*(square2[0]+square2[2])};

            if(result1[0]<result3[0]){
                result[0]=result1[0];
                result[1]=result1[1];
            }else{
                result[0]=result3[0];
                result[1]=result3[1];
            }
            if(result2[0]<result4[0]){
                result[2]=result4[0];
                result[3]=result4[1];
            }else{
                result[2]=result2[0];
                result[3]=result2[1];
            }
        }else if(k<-1){
            double[] result2=new double[]{(square1[1]-b)/k,square1[1]};
            double[] result1=new double[]{(square1[1]+square1[2]-b)/k,square1[1]+square1[2]};
            double[] result4=new double[]{(square2[1]-b)/k,square2[1]};
            double[] result3=new double[]{(square2[1]+square2[2]-b)/k,square2[1]+square2[2]};

            if(result1[0]<result3[0]){
                result[0]=result1[0];
                result[1]=result1[1];
            }else{
                result[0]=result3[0];
                result[1]=result3[1];
            }
            if(result2[0]<result4[0]){
                result[2]=result4[0];
                result[3]=result4[1];
            }else{
                result[2]=result2[0];
                result[3]=result2[1];
            }
        }
        else{
            double[] result1=new double[]{(square1[1]-b)/k,square1[1]};
            double[] result2=new double[]{(square1[1]+square1[2]-b)/k,square1[1]+square1[2]};
            double[] result3=new double[]{(square2[1]-b)/k,square2[1]};
            double[] result4=new double[]{(square2[1]+square2[2]-b)/k,square2[1]+square2[2]};

            if(result1[0]<result3[0]){
                result[0]=result1[0];
                result[1]=result1[1];
            }else{
                result[0]=result3[0];
                result[1]=result3[1];
            }
            if(result2[0]<result4[0]){
                result[2]=result4[0];
                result[3]=result4[1];
            }else{
                result[2]=result2[0];
                result[3]=result2[1];
            }
        }
        return result;

    }
}
